package com.songshuai.sales.client.service.impl;

import com.songshuai.sales.client.feign.PanguApiClient;
import com.songshuai.sales.client.service.CommonService;
import com.songshuai.webapi.baseapi.dto.feign.response.DictionaryDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: lhx
 * @Date: 2019/3/28 15:24
 */
@Service
public class CommonServiceImpl implements CommonService {


    @Value("${file.basePath}")
    private String basePath;

    @Value("${file.imgPath}")
    private String imgPath;

    @Value("${file.imgMaxSize}")
    private int imgMaxSize;


    @Autowired
    private PanguApiClient panguApiClient;



    /**
     * 根据字典类型查询字典
     * @param type
     * @return
     */
    @Override
    public QueryResponse<List<DictionaryDto>> queryDictionariesByType(Integer type) {
        QueryResponse<List<DictionaryDto>> listQueryResponse = panguApiClient.queryDictionariesByType(type);
        if (null != listQueryResponse && ResultStatus.SUCCESS.getErrorCode() == listQueryResponse.getCode()) {
            return listQueryResponse;
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 根据id查询字典详情
     * @param id
     * @return
     */
    @Override
    public QueryResponse<DictionaryDto> queryDictionaryDetail(Integer id) {
        QueryResponse<DictionaryDto> queryResponse = panguApiClient.queryDictionaryDetail(id);
        if (null != queryResponse && ResultStatus.SUCCESS.getErrorCode() == queryResponse.getCode()) {
            return queryResponse;
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 图片上传（七牛云）
     * @param file
     * @return
     */
    @Override
    public QueryResponse uploadPicture(MultipartFile file) {
        QueryResponse queryResponse = panguApiClient.uploadPicture(file,imgMaxSize,basePath,imgPath);
        if (null != queryResponse && ResultStatus.SUCCESS.getErrorCode() == queryResponse.getCode()) {
            return queryResponse;
        }
        return new QueryResponse<>(ResultStatus.ERROR_PIC_UPLOAD);
    }


    /**
     * 图片上传（阿里云）
     * @param file
     * @return
     */
    @Override
    public QueryResponse uploadPictureOss(MultipartFile file) {
        QueryResponse queryResponse = panguApiClient.uploadPictureOss(file);
        if (null != queryResponse && ResultStatus.SUCCESS.getErrorCode() == queryResponse.getCode()) {
            return queryResponse;
        }
        return new QueryResponse<>(ResultStatus.ERROR_PIC_UPLOAD);
    }


}
